package com.zzw.model;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class User {

	private String userName;
	private String password;
	
	public User(){
		System.out.println("new user");
	}
	
	public User(String userName, String password) {
		super();
		this.userName = userName;
		this.password = password;
	}
	
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	public static void printClassInfo(Object object){
		Class c = object.getClass();
		Method[] methods = c.getMethods();
		for(int i = 0;i < methods.length;i++){
			Class returnType = methods[i].getReturnType();
			System.out.println(returnType.getName());
			System.out.println(methods[i].getName() + "(");
			Class[] parameterTypes = methods[i].getParameterTypes();
			for(Class class1:parameterTypes){
				System.out.println(class1.getName() + ",");
			}
			System.out.println(")");
		}
	}
	
	public static void printFiledInfo(Object object){
		Class c = object.getClass();
		Field[] fileds = c.getDeclaredFields();
		for(Field field:fileds){
			Class filedType = field.getType();
			System.out.println(filedType.getName() + "---" + field.getName());
		}
	}
	
	public void add(int a,int b){
		System.out.print(a+b);
	}    
	
	public void toUpper(String a){
		System.out.print(a.toUpperCase());
	}
	
	public static void printConstructInfo(Object object){
		Class c = object.getClass();
		Constructor[] constructors = c.getDeclaredConstructors();
		for(Constructor constructor:constructors){
			System.out.print(constructor.getName()+"(");
			Class[] typeParas=constructor.getParameterTypes();
            for (Class class1:typeParas){
                System.out.print(class1.getName()+" ,");
            }
    		System.out.println(")");
		}
		
	}
	
	public static void main(String[] args) {
		User user = new User();
		Class c1 = User.class;
		Class c2 = user.getClass();
		try {
			Class c3 = Class.forName("com.zzw.model.User");
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			User u = (User) c1.newInstance();
			System.out.println(c1.getName());
			System.out.println(c1.getSimpleName());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		printClassInfo(user);
		printFiledInfo(user);
		printConstructInfo(user);

		User test = new User();
		Class c = test.getClass();
		try {
			Method method = c.getMethod("add",new Class[]{int.class,int.class});
			method=c.getMethod("add",int.class,int.class);
		    method.invoke(test,10,10);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}